from PIL import Image

def pixmap_to_image(pixmap):
    # 将Pixmap对象转换为灰度模式的PIL Image对象
    data = pixmap.samples  # 获取像素数据
    width, height = pixmap.width, pixmap.height  # 获取图像尺寸
    image = Image.frombytes("RGB", (width, height), data)
    return image.convert('L')

def compare_two_image(image1,image2):
    if image1.size != image2.size:
        raise ValueError("Images have different dimensions.")
    
    # 计算两张图片的像素差异
    difference = sum([abs(a - b) for a, b in zip(image1.getdata(), image2.getdata())])
    
    # 计算相似度分数，差异越小，相似度越高
    similarity_score = 1 - (difference / (255 * image1.size[0] * image1.size[1]))
    return similarity_score